﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PersistentObject.Common
{
    public class SchemaConfig
    {
        #region mapping configuration
        internal Dictionary<Type, string> TableMappingDictionary = new Dictionary<Type, string>();
        internal object TableMappingLockObject = new object();
        public void SetTableMapping(Type type, string table)
        {
            lock (TableMappingLockObject)
            {
                if (TableMappingDictionary.ContainsKey(type))
                {
                    TableMappingDictionary.Add(type, table);
                }
                else
                {
                    TableMappingDictionary[type] = table;
                }
            }
        }
        public void SetTableMapping<T>(string table)
        {
            var type = typeof(T);
            SetTableMapping(type, table);
        }

        public void RemoveTableMapping(Type type)
        {
            lock (TableMappingLockObject)
            {
                if (TableMappingDictionary.ContainsKey(type))
                {
                    TableMappingDictionary.Remove(type);
                }
            }
        }
        public void RemoveTableMapping<T>()
        {
            var type = typeof(T);
            RemoveTableMapping(type);
        }

        public string GetTableMapping<T>()
        {
            Type type = typeof(T);
            return GetTableMapping(type);
        }
        public string GetTableMapping(Type type)
        {
            lock (TableMappingLockObject)
            {
                if (TableMappingDictionary.ContainsKey(type))
                {
                    return TableMappingDictionary[type];
                }
                else
                {
                    return null;
                }
            }
        }

        internal Dictionary<Type, Type> TypeMappingDictionary = new Dictionary<Type, Type>();
        public void SetTypeMapping(Type beReplacedType, Type targetType)
        {
            if(TypeMappingDictionary.ContainsKey(beReplacedType))
            {
                TypeMappingDictionary[beReplacedType] = targetType;
            }
            else
            {
                TypeMappingDictionary.Add(beReplacedType, targetType);
            }
        }
        public Type GetTypeMapping(Type beReplacedType)
        {
            if(TypeMappingDictionary.ContainsKey(beReplacedType))
            {
                return TypeMappingDictionary[beReplacedType];
            }
            else
            {
                return beReplacedType;
            }
        }
        #endregion


    }
}
